<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once APPPATH . 'controllers/application' . EXT;
require_once APPPATH . 'controllers/main' . EXT;

class User extends Application {

    public $main;

    public function __construct() {
        parent::__construct();
    }

    public function view_login() {
        $data = array();
        $this->render($this->load->view('login_user_view', $data, TRUE));
    }

    public function login_in() {
        $this->form_validation->set_rules('username', 'Tên đăng nhập', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'trim|required|xss_clean|callback_check_database');
        if ($this->form_validation->run() == false) {
//            echo validation_errors();
            $infor = array(
                'errors' => validation_errors()
            );
            echo json_encode($infor);
            die;
        } else if ($this->session->userdata('logged_in')) {
            $infor = array(
                'errors' => true
            );
            echo json_encode($infor);
            die;
        } else {
            $infor = array(
                'errors' => true
            );
            echo json_encode($infor);
            die;
        }
    }

    public function check_database($password) {
        $username = $this->input->post('username');
        $result = $this->user_model->login($username, $password);
        if ($result) {
            $sess_array = array();
            foreach ($result as $row) {
                $sess_array = array(
                    'id' => $row->CID,
                    'username' => $row->CAcount
                );
                $this->session->set_userdata('logged_in', $sess_array);
            }
            return true;
        } else {
            $this->form_validation->set_message('check_database', 'Tên đăng nhập hoặc mật khẩu không đúng');
            return false;
        }
    }

    public function login_out() {
        $this->session->unset_userdata('logged_in');
        redirect($this->session->userdata('previous_page'));
    }

//quen mat khau
    public function quen_mk() {
        if (!$this->session->userdata('logged_in')) {
            $this->render($this->load->view("quen_mat_khau_view", "", TRUE));
        } else {
            $this->main = new Main();
            $this->main->index();
        }
    }

//xu ly quen mat khau
    public function xl_quen_mk() {
        $config2 = array(
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email|xss_clean|callback_check_email'
            ),
            array(
                'field' => 'answers',
                'label' => 'Câu trả lời',
                'rules' => 'trim|required|xss_clean|callback_check_quest'
            ),
            array(
                'field' => 'password',
                'label' => 'Mật khẩu',
                'rules' => 'trim|required|xss_clean'
            ),
            array(
                'field' => 'repassword',
                'label' => 'Nhập lại mật khẩu',
                'rules' => 'trim|xss_clean|required|matches[password]|'
            )
        );
        $this->form_validation->set_rules($config2);
        if ($this->form_validation->run() === FALSE) {
//            $this->quen_mk();
//            redirect('dang_ky', 'refresh');
//            echo validation_errors();
            $dk = array(
                'er_email' => form_error("email"),
                'er_answers' => form_error("answers"),
                'er_pass' => form_error("password"),
                'er_repass' => form_error("repassword"),
                'errors3' => FALSE
            );
            echo json_encode($dk);
            die;
        } else {
            $query = array(
                "email" => $this->input->post('email'),
                "pass" => $this->input->post('password')
            );
            $rs = $this->user_model->QuenMK($query);
            if ($rs) {
//                    $this->render($this->load->view('dk_thanh_cong', '', true));
                $dk = array(
                    'errors3' => TRUE
                );
                echo json_encode($dk);
                die;
            }
        }
    }

    public function check_email($email) {
        $result = $this->user_model->GetEmail($email);
        if ($result) {
            return TRUE;
        } else {
            $this->form_validation->set_message('check_email', 'Email không tồn tại');
            return FALSE;
        }
    }

    public function check_quest($asw) {
        $email = $this->input->post("email");
        $result = $this->user_model->CheckQ($asw, $email);
        if ($result) {
            return TRUE;
        } else {
            $this->form_validation->set_message("check_quest", "Câu trả lời không đúng hoặc không thuộc email này");
            return FALSE;
        }
    }
}

?>
